//
//  ZJPhotographSelector.m
//  DreamSoftReimburse
//
//  Created by ZJ on 17/4/23.
//  Copyright © 2017年 ZJ. All rights reserved.
//

#import "ZJPhotographSelector.h"
#import "ZJAdditions.h"

@interface ZJPhotographSelector ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
UIImagePickerController *_imagePickerController;/**< 图片拾取控制器，课通过该类访问相册，拍照和录影，要使用图片拾取控制器，需要同时遵循 UINavigationControllerDelegate和UIImagePickerControllerDelegate 协议代理*/
}
@end

static ZJPhotographSelector *__photographSelector;
@implementation ZJPhotographSelector

+ (void)initialize
{
    [ZJPhotographSelector sharedPhotographSelector];
}

+ (instancetype)alloc{
    if (__photographSelector) {
        return __photographSelector;
    }
    return [super alloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _imagePickerController = [[UIImagePickerController alloc]init];
        _imagePickerController.delegate = self;
    }
    return self;
}
+ (instancetype)sharedPhotographSelector{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __photographSelector = [[ZJPhotographSelector alloc] init];
    });
    return __photographSelector;
}


#pragma mark - 相机
- (void)corporationClick{
    //弹窗提示用户
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
   UIAlertAction * action =  [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //进入相册
        [self accessPhotoALbm];
   }];
   UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self takingPictures];
    }];
    
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:action];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [[self topViewController] presentViewController:alertController animated:YES completion:nil];
}

//相册
- (void)accessPhotoALbm{
    //1、获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    //2、判断是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        //3、设置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //4、设置媒体格式
        _imagePickerController.mediaTypes = @[mediaTypes.firstObject];
        //5、设置是否允许编辑  如果该属性为NO 就不会进入到照片的编辑界面
        _imagePickerController.allowsEditing = YES;
        //6、推送
        [[self topViewController] presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{

        [YJProgressHUD showMsgWithoutView:@"设备不支持访问相册"];
    }
}
//拍照
- (void)takingPictures{
    
    //1、获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //2、判断是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //3、设置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 数组内的第一个元素是照相，第二个是录像 */
        //4、设置媒体格式
        _imagePickerController.mediaTypes = @[mediaTypes.firstObject];
        //5、设置相机模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        //6、设置摄像头（前置、后置）
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        //7、设置闪光灯
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        //8、推送
        [[self topViewController] presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
   
         [YJProgressHUD showMsgWithoutView:@"设备不支持访问拍照"];
    }
    
}


#pragma mark UIImagePickerControllerDelegate协议方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    if (self.delegate) {
        [self.delegate imagePickerController:picker didFinishPickingMediaWithInfo:info];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}


@end
